package com.noahplat.person.controller;

import com.noahplat.common.utils.Result;
import com.noahplat.person.pojo.PersonDevice;
import com.noahplat.person.service.PersonDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @ClassName: PersonDeviceController
 * @Description: 人员-设备绑定关系controller
 * @Author 李景星
 * @Date 2021/3/9
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/personDevice")
@Validated
public class PersonDeviceController {

    @Autowired
    private PersonDeviceService personDeviceService;

    /**
     * 添加人员和设备绑定关系
     * @param imei
     * @param devcode
     * @param pid
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-person:PersonDeviceController:addPersonDevice')")
    @PostMapping("/addPersonDevice")
    public Result addPersonDevice(@NotEmpty(message = "设备IMEI不能为空") String imei,
                                  @NotEmpty(message = "设备激活码不能为空") String devcode,
                                  @NotEmpty(message = "移动设备注册ID不能为空") String registrationid,
                                  @NotEmpty(message = "设备类型标识不能为空") String devtype,
                                  @NotNull(message = "用户ID不能为空 ") @Min(value = 1, message = "人员ID不合法") Long pid){
        return personDeviceService.addPersonDevice(imei, devcode, pid,registrationid,devtype);
    }


    /**
     * 查询人员绑定设备
     * @param pid
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-person:PersonDeviceController:getPersonDevice')")
    @GetMapping("/getPersonDevice")
    public Result getPersonDevice(@NotNull(message = "人员ID不能为空") @Min(value = 1, message = "人员ID不合法") Long pid){
        return personDeviceService.getPersonDevice(pid);
    }

    /**
     * 解除人员绑定
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-person:PersonDeviceController:delPersonDevices')")
    @DeleteMapping("/delPersonDevice")
    public Result delPersonDevice(@NotNull(message = "ID不能为空") @Min(value = 1, message = "ID不合法") Long id){
        return personDeviceService.delPersonDevice(id);
    }

    /**
     * 修改人员设备绑定关系
     * @param personDevice
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-person:PersonDeviceController:updatePersonDevice')")
    @PutMapping("/updatePersonDevice")
    public Result updatePersonDevice(@RequestBody PersonDevice personDevice){
        return personDeviceService.updatePersonDevice(personDevice);
    }

    /**
     * 获取默认设备
     * @param pid
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-person:PersonDeviceController:getDefaultDevice')")
    @GetMapping("/getDefaultDevice")
    public Result getDefaultDevice(@NotNull(message = "人员ID不能为空") @Min(value = 1, message = "人员ID不合法") Long pid){
        return personDeviceService.getDefaultDevice(pid);
    }

    /**
     * 手机和设备绑定关系修改
     * @param pid
     * @param registrationid
     * @param devtype
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-person:PersonDeviceController:updateRegis')")
    @PutMapping("/updateRegis")
    public Result updateRegis(@NotNull(message = "人员ID不能为空") @Min(value = 1, message = "人员ID不合法") Long pid, @NotNull(message = "移动设备注册ID不能为空") String registrationid,  @NotNull(message = "设备类型标识不能为空") String devtype){
        return personDeviceService.updateRegis(pid,registrationid,devtype);
    }

    /**
     * 查询内存操作方法
     * 宋彦磊
     * @param imei
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-person:PersonDeviceController:inRedis')")
    @PostMapping("/inRedis")
    public Result inRedis(String imei){
        return personDeviceService.inRedis(imei);
    }

}
